package com.eagle.client;


import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.event.StoreListenerAdapter;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;
import com.ssc.osa.osawt.workspace.client.ComponentAssistant;
import com.ssc.osa.osawt.workspace.client.FileUploadDialog;
import com.ssc.osa.osawt.workspace.client.GridPanel;
import com.ssc.osa.osawt.workspace.client.auth.MCH;
import com.ssc.osa.osawt.workspace.client.core.AjaxUtil;
import com.ssc.osa.osawt.workspace.client.core.IntrefOutput;
import com.ssc.osa.osawt.workspace.client.core.IntrefParam;
import com.ssc.osa.osawt.workspace.client.data.Store;
import com.ssc.osa.osawt.workspace.client.util.Formatter;

public class OWT_223_Import extends ComponentAssistant {

	private FileUploadDialog dialog;
	private FileImportStoreListner lstnFileImport;
	private IntrefParam[] importInputs;
	private IntrefOutput[] importOutputs;
	private IntrefOutput[] gridOutputs;
	private GridPanel grdPanel;
	private final static String DEC6_FORMAT = "###,###,###,##0.000000";
	
	public OWT_223_Import() {
		super(new MCH());
		setWestPanelVisible(false);
		setEastPanelVisible(false);
		setSouthPanelVisible(false);
	}

	public Panel getComponent() {
		Panel pnlMain = new Panel();
		pnlMain.setLayout(new FitLayout());
		pnlMain.setTitle("OWT-223");

		gridOutputs = new IntrefOutput[] {
				
			new IntrefOutput("Staus Code", "OVR_STATUS_CD", String.class, 80, true),// hidden
			new IntrefOutput("Fund ID", "FUND_ID", String.class, 50),
			new IntrefOutput("Calc Basis Code", "CLC_BASIS_CD", String.class, 100),
			new IntrefOutput("Issue ID", "ESM_ISSUE_ID", String.class, 100),
			new IntrefOutput("Issue Account Number", "ISSUE_ACCT_NUM", String.class, 80),
			new IntrefOutput("Lot Number", "LOT_NUM", String.class, 80),
			new IntrefOutput("Curr Code", "LOC_CURR_CD", String.class, 60),
			new IntrefOutput("Long Short Code", "LONG_SHORT_CD", String.class, 70),
			new IntrefOutput("Last Amortization Date", "LAST_AMORT_DT", Date.class, 90, null, "Ynd", "MM/dd/yyyy"),
			new IntrefOutput("Account Num", "ACCOUNT_NUM", Integer.class, 80),
			new IntrefOutput("Trade Date", "TRADE_DT", Date.class, 90, null, "Ynd", "MM/dd/yyyy"),
			new IntrefOutput("Trade ID", "TRADE_ID", String.class, 100),
			new IntrefOutput("Amortization Exchange Rate", "XCHG_RT", Double.class, 100, Formatter.getDecimalRenderer(DEC6_FORMAT)),
		 	new IntrefOutput("Share Num", "PAR_SHR_NUM", Double.class, 100),
			new IntrefOutput("Base Original Cost", "ORIG_COST_ID_BAM", Double.class, 100),
			new IntrefOutput("Local Original Cost", "ORIG_COST_ID_LAM", Double.class, 100),
			new IntrefOutput("Base Amortized Cost", "COST_ID_BAM", Double.class, 100),
			new IntrefOutput("Local Amortized Cost", "COST_ID_LAM", Double.class, 100),
			new IntrefOutput("Local OID Amortization", "OI_TOT_AMR_ICM_LAM", Double.class, 100),
			new IntrefOutput("Base OID Amortization", "OI_TOT_AMR_ICM_BAM", Double.class, 100),
			new IntrefOutput("Local Market Amortization", "M_TOT_AMR_ICM_LAM", Double.class, 100),
			new IntrefOutput("Base Market Amortization", "M_TOT_AMR_ICM_BAM", Double.class, 100),
			new IntrefOutput("OID Currency Gain/Loss", "OI_CP_CURR_GL_AMT", Double.class, 100),
			new IntrefOutput("Market Currency Gain/Loss", "MKT_CP_CURR_GL_AMT", Double.class, 100),
			new IntrefOutput("Event Type Code", "EVENT_TYPE_CD", String.class, 100),
			new IntrefOutput("Short Txt", "ERROR_SHORT_TXT", String.class, 100)
			

		};

		int enablePag = GridPanel.PAGINATION | GridPanel.SORT
				| GridPanel.FILTER | GridPanel.EXCEL_EXPORT;

		grdPanel = new GridPanel(gridOutputs, 1000, 295, enablePag, 25);
		GWT.log("grdPanel.getRId()  - " +grdPanel.getRId(),null);
		
		grdPanel.setBorder(false);

		//grdPanel.setRId(new Date().getTime() + "");
		grdPanel.setId("123");
		grdPanel.setInitialSort("-PROFILE_NM");
		pnlMain.add(grdPanel);

		lstnFileImport = new FileImportStoreListner(grdPanel);
		
		GWT.log(IntrefOutput.getMetaDataString(gridOutputs,true) , null);
		

		Button btnUpload = new Button("Upload -- File");
		btnUpload.addListener(new OWT_223BtnListener());

		pnlMain.addButton(btnUpload);

		return pnlMain;
	}

	public FileUploadDialog getDialog() {
		return dialog;
	}

	public void setDialog(FileUploadDialog dialog) {
		this.dialog = dialog;
	}

	private class OWT_223BtnListener extends ButtonListenerAdapter {
		public void onClick(Button button, EventObject e) {			
			grdPanel.doImport(1, gridOutputs, "csv");
		}

		public void onClick1(Button button, EventObject e) {
			
			String request = "";
			request += "SOURCE=FUND_ID|TYPE=string|FORMAT=$";
			request += "SOURCE=CLC_BASIS_CD|TYPE=string|FORMAT=$";
			request += "SOURCE=ESM_ISSUE_ID|TYPE=string|FORMAT=$";
			request += "SOURCE=ISSUE_ACCT_NUM|TYPE=int|FORMAT=$";
			request += "SOURCE=LOT_NUM|TYPE=string|FORMAT=$";
			request += "SOURCE=LOC_CURR_CD|TYPE=string|FORMAT=$";
			request += "SOURCE=LONG_SHORT_CD|TYPE=string|FORMAT=$";
			request += "SOURCE=CONV_DT|TYPE=date|FORMAT=$";
			request += "SOURCE=ACCOUNT_NUM|TYPE=int|FORMAT=$";
			request += "SOURCE=TRADE_ID|TYPE=string|FORMAT=$";
			request += "SOURCE=TRADE_DT|TYPE=date|FORMAT=$";
			request += "SOURCE=XCHG_RT|TYPE=double|FORMAT=##0.000000000$";
			request += "SOURCE=PAR_SHR_NUM|TYPE=double|FORMAT=###,###,###,##0.00000$";
			request += "SOURCE=ORIG_COST_ID_BAM|TYPE=double|FORMAT=$";
			request += "SOURCE=ORIG_COST_ID_LAM|TYPE=double|FORMAT=$";
			request += "SOURCE=COST_ID_BAM|TYPE=double|FORMAT=$";
			request += "SOURCE=COST_ID_LAM|TYPE=double|FORMAT=$";
			request += "SOURCE=OI_TOT_AMR_ICM_LAM|TYPE=double|FORMAT=$";
			request += "SOURCE=OI_TOT_AMR_ICM_BAM|TYPE=double|FORMAT=$";
			request += "SOURCE=M_TOT_AMR_ICM_LAM|TYPE=double|FORMAT=$";
			request += "SOURCE=M_TOT_AMR_ICM_BAM|TYPE=double|FORMAT=$";
			request += "SOURCE=OI_CP_CURR_GL_AMT|TYPE=double|FORMAT=$";
			request += "SOURCE=MKT_CP_CURR_GL_AMT|TYPE=double|FORMAT=$";
			request += "SOURCE=EVENT_TYPE_CD|TYPE=string|FORMAT=$";
			request += "SOURCE=ERROR_SHORT_TXT|TYPE=string|FORMAT=$";
			request += "SOURCE=OVR_STATUS_CD|TYPE=string|FORMAT=$";

			importInputs = new IntrefParam[] {
					new IntrefParam("__request", "130"),
					new IntrefParam("TARGET_FILES", "${TEMP_FILE}"),
					new IntrefParam("USER_FILES", "${USER_FILE}"),
					new IntrefParam("R_ID", grdPanel.getRId()),
					new IntrefParam("__IMPORT_META_CONFIG", request) };
			importOutputs = new IntrefOutput[] { new IntrefOutput("__STATUS",
					String.class) };
			dialog = new FileUploadDialog(3, importInputs, importOutputs,
					lstnFileImport);
			dialog.setPermittedExtensions(new String[] { "csv" });
			dialog.show();
		}
	}

	private class FileImportStoreListner extends StoreListenerAdapter {
		private GridPanel grdPanel;
		private IntrefParam[] grdInputs;

		public FileImportStoreListner(GridPanel grdPanel) {
			super();
			setGrdPanel(grdPanel);
		}

		public void onLoad(com.gwtext.client.data.Store store, Record[] records) {
			grdPanel.loadFromCache();
			MessageBox.alert(grdPanel.getCacheUrl());
			//processCacheUrl();
		}

		public GridPanel getGrdPanel() {
			return grdPanel;
		}

		public void setGrdPanel(GridPanel grdPanel) {
			this.grdPanel = grdPanel;
		}

		public IntrefParam[] getGrdInputs() {
			return grdInputs;
		}

		public void setGrdInputs(IntrefParam[] grdInputs) {
			this.grdInputs = grdInputs;
		}
	}

	public FileImportStoreListner getLstnFileImport() {
		return lstnFileImport;
	}

	public void setLstnFileImport(FileImportStoreListner lstnFileImport) {
		this.lstnFileImport = lstnFileImport;
	}

	public IntrefOutput[] getImportOutputs() {
		return importOutputs;
	}

	public void setImportOutputs(IntrefOutput[] importOutputs) {
		this.importOutputs = importOutputs;
	}

	private void processCacheUrl() {
		// MessageBox.alert(getRId());

		IntrefOutput[] caheUrlStoreOuts = new IntrefOutput[] { new IntrefOutput(
				"__status", String.class) };

		Store caheUrlStore = new Store(caheUrlStoreOuts);

		IntrefParam[] params = new IntrefParam[] {

				new IntrefParam("__request", "10000"),
				new IntrefParam("URL", AjaxUtil.urlEncode(grdPanel
						.getCacheUrl()
						+ "&__type=java")) };

		caheUrlStore.load(params);
	}
}
